from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.paginator import Paginator
from django.contrib.auth.hashers import check_password, make_password
import time
import math
from .models import *
from .ser import *


# Create your views here.


class SshopM(APIView):
    def get(self, request):
        id = request.GET.get('id')
        if not id:
            shop = Sshop.objects.all()
            ser = SshopSer(shop, many=True)
            return Response({'code': 200, 'list': ser.data})
        else:
            shop = Sshop.objects.filter(id=id).first()
            userid = request.GET.get('userid')
            ser = SshopSer(shop)
            print(userid)
            flag = False
            user = shop.user.all()
            for i in user:
                if int(userid) == int(i.id):
                    flag = True

            return Response({'code': 200, 'list': ser.data, 'flag': flag})

    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Suser.objects.filter(username=username).first()
        if user:
            if password == user.password:
                return Response({'code': 200, 'userid': user.id, 'types': user.types})
            else:
                return Response({'code': 20010, 'mes': '密码错误'})
        else:
            return Response({'code': 20020, 'mes': '用户名错误'})


class ScomM(APIView):
    def post(self, request):
        ser = ScomSer(data=request.data)
        if ser.is_valid():
            ser.save()
            user = Suser.objects.filter(id=request.data.get('user')).first()
            if user.types == 1:
                user.num += 10
                user.save()
            elif user.types == 2:
                user.num += 5
                user.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self, reuqest):
        user = Suser.objects.all()
        ser = SuserSer(user, many=True)
        return Response({'code': 200, 'list': ser.data})


class Shouc(APIView):
    def post(self, request):
        userid = request.data.get('userid')
        shopid = request.data.get('shopid')
        flag = request.data.get('flag')
        user = Suser.objects.filter(id=userid).first()
        shop = Sshop.objects.filter(id=shopid).first()
        if flag == True:
            user.users.remove(shop)
            flag = False
            shop.save()
        else:
            user.users.add(shop)
            flag = True
            shop.save()
        return Response({'code': 200, 'flag': flag})


class AuM(APIView):
    def post(self, request):
        request.data['password'] = make_password(request.data.get('password'))

        ser = AuSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self, request):
        au = Author.objects.all()
        ser = AuSer(au, many=True)
        return Response({'code': 200, 'list': ser.data})


class AbookM(APIView):
    def post(self, request):
        ser = ABSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self, request):
        name = request.GET.get('name')
        if not name:
            cpage = request.GET.get('cpage')
            book = Abook.objects.all()
            p = Paginator(book, 3)
            books = p.page(cpage)
            ser = SABSer(books, many=True)
            return Response({'code': 200, 'list': ser.data, 'tpage': p.num_pages})
        else:
            book = Abook.objects.filter(name=name).all()
            ser = SABSer(book, many=True)
            return Response({'code': 200, 'list': ser.data})


class Login(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Author.objects.filter(username=username).first()
        if username == username:
            if check_password(password, user.password):
                return Response({'code': 200, 'userid': user.id, 'types': user.types})
            else:
                return Response({'code': 20010, 'mes': '密码错误'})
        else:
            return Response({'code': 20011, 'mes': '用户名错误'})


class MovieM(APIView):
    def post(self, request):
        ser = MovieSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self, request):

        movies = Movie.objects.all().order_by('mtime')
        ser = MovieSer(movies, many=True)
        return Response({'code': 200, 'list': ser.data})


class YanM(APIView):
    def post(self, request):
        ser = ActorSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self, request):

        actor = Actor.objects.all().order_by('age')
        ser = ActorSer(actor, many=True)
        return Response({'code': 200, 'list': ser.data})


class SHOU(APIView):
    def post(self, request):
        mid = request.data.get('mid')
        yid = request.data.get('yid')
        movie = Movie.objects.filter(id=mid).first()
        yan = Actor.objects.filter(id=yid).first()
        movie.yan.add(yan)
        movie.save()
        return Response({'code': 200})
